<style>
.selectwords a,.selectwords .btn-group{
	display:inline-block;
	margin:5px;
}
.container{width:100% !important;}
.cate-item{margin:0 10px;}
</style>
<div id="{{$pageID}}">
<form>
<div class="form-inline">

<a href="?cache=no&{{ http_build_query($_GET)}}" class="btn btn-primary btn-xs">更新标签缓存</a>

<input type="text" class="form-control" style="height:26px;margin-right:5px;" id="{{$pageID}}-searchword" ><button class="btn btn-primary btn-xs" id="{{$pageID}}-search">{{__('Search')}}</button>
<p class="alert alert-warning"><span>点击待选词选中即可，标签按分类以及汉字拼音排序</span></p>
</div>

<div id="{{$pageID}}-selected" class="selectwords clearfix">
<?php
$ids = explode(',',$_REQUEST['ids']);
?>
{{loop $select_items $item}}
	{{if in_array($item[$current_model]['id'],$ids)}}
		<div class="btn-group" rel="{{$item[$current_model]['id']}}" relword="{{$item[$current_model]['name']}}">
				<input type="hidden" name="data[{{$current_model}}][{{$item[$current_model]['id']}}][{{$associd}}]" value="{{$item[$current_model]['id']}}">
				<input type="hidden" name="data[{{$current_model}}][{{$item[$current_model]['id']}}][relatedmodel]" value="{{$relatedmodel}}">
				<button type="button" class="btn btn-primary">{{$item[$current_model]['name']}}</button>
				<button type="button"  rel="{{$item[$current_model]['id']}}" class="btn btn-primary btn-remove"><i class="glyphicon glyphicon-remove"></i></button>
		</div>
	{{/if}}
{{/loop}}
</div>

<div id="{{$pageID}}-words" class="selectwords clearfix">
{{loop $cate_names $cate_id $cate_name}}
	{{if !empty($cate_items[$cate_id])}}
		<div class="clearfix">
		<strong>{{$cate_name}} : </strong> 
		{{loop $cate_items[$cate_id] $item}}
			<a class="cate-item" href="javascript:void(0);" rel="{{$item[$current_model]['id']}}" type="button">{{$item[$current_model]['name']}}</a>
		{{/loop}}
		</div>
		<hr>
	{{/if}}
{{/loop}}
<!-- 按分类排列 -->
	</div>
	
	{{$page_navi}}
	<hr style="margin:10px 0;"/>
	
	<div class="submit"><input type="submit" class="btn btn-warning" onclick="window.close();" value="关闭"></div>
</form>
</div>

<script>
$(function(){
	$('#{{$pageID}}-search').on('click',function(){
		var text = $('#{{$pageID}}-searchword').val();
		var dialog = $(this).closest('.ui-dialog-content');
		var url='{{Router::url("/admin/$current_model/list?type=select&m=$relatedmodel&targetid=$targetid")}}';
		url +='&val='+encodeURIComponent(text);
		if(dialog.size()>0){
			dialog.load(url);
		}
		else{
			window.location.href=url;
		}
	})
	//$(document).on('')
	$('#{{$pageID}}-words').on('click','a',function(){
		var rel = $(this).attr('rel');
		var html = '<div class="btn-group" rel="'+$(this).attr('rel')+'" relword="'+$(this).html()+'"> \
				<input type="hidden" name="data[{{$current_model}}]['+$(this).attr('rel')+'][{{$associd}}]" value="'+$(this).attr('rel')+'">\
				<input type="hidden" name="data[{{$current_model}}]['+$(this).attr('rel')+'][relatedmodel]" value="{{$relatedmodel}}">\
				<button type="button" class="btn btn-primary">'+$(this).html()+'</button>\
				<button type="button" class="btn btn-primary btn-remove"><i class="glyphicon glyphicon-remove"></i></button></div>'
		
		if('{{$targetid}}'!=''){
			var target_obj = $('#{{$targetid}}');
			if(window.opener){
				target_obj = window.opener.document.getElementById('{{$targetid}}');
			}
			
			var exist_flag = false;
			$(target_obj).find('.btn-group,.label').each(function(){
				if($(this).attr('rel')==rel){
					exist_flag = true;
				}
			});
			if(!exist_flag){
				{{if $_REQUEST['operate']=='edit'}}
				ajaxAction('/admin/{{$relatedmodel}}/addtag/{{$_REQUEST['mid']}}/'+$(this).attr('rel'),null,null,function(){
					$(html).appendTo($(target_obj)); 
				});
				{{else}}
				$(html).appendTo($(target_obj)); //不存在时，插入
				{{/if}}
			}
		}
		$(html).appendTo('#{{$pageID}}-selected');
		$(this).remove();
	});
	// click绑定到<i></i>无效，绑定到上级的btn
	$('#{{$pageID}}-selected').on('click','.btn-remove',function(){
		var obj = $(this).closest('.btn-group');
		var html = '<a href="javascript:;" rel="'+obj.attr('rel')+'" class="btn btn-xs btn-info" type="button">'+obj.attr('relword')+'</a>'
		$(html).appendTo('#{{$pageID}}-words');
		obj.remove();
		
		{{if $_REQUEST['operate']=='edit'}}
		ajaxAction('/admin/{{$relatedmodel}}/deletetag/{{$_REQUEST['mid']}}/'+$(this).attr('rel'),null,null,function(){
			; 
		});
		{{/if}}
		
		if('{{$targetid}}'!=''){
			var target_obj = $('#{{$targetid}}');
			if(window.opener){
				target_obj = window.opener.document.getElementById('{{$targetid}}');
			}
			$(target_obj).find('.btn-group,.label').each(function(){
				if($(this).attr('rel')==obj.attr('rel')){
					$(this).remove();
				}
			});
		}
	});
	
})
</script>